package com.ruoyi.post.service.impl;


import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.experiment.domain.Circulation;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.mapper.ExperimentMapper;
import com.ruoyi.post.domain.CanDo;
import com.ruoyi.post.domain.JobInformation;
import com.ruoyi.post.mapper.JobInformationMapper;
import com.ruoyi.post.model.*;
import com.ruoyi.post.service.IJobInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 岗位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-09
 */
@Service
public class JobInformationServiceImpl implements IJobInformationService
{
    @Autowired
    private JobInformationMapper jobInformationMapper;
    @Autowired
    private ExperimentMapper experimentMapper;

    /**
     * 查询岗位信息
     *
     * @param jobId 岗位信息主键
     * @return 岗位信息
     */
    @Override
    public JobInformation selectJobInformationByJobId(Long jobId)
    {
        return jobInformationMapper.selectJobInformationByJobId(jobId);
    }

    /**
     * 查询岗位信息列表
     *
     * @param jobInformation 岗位信息
     * @return 岗位信息
     */
    @Override
    public List<JobInformation> selectJobInformationList(JobInformation jobInformation)
    {
        return jobInformationMapper.selectJobInformationList(jobInformation);
    }

    /**
     * 新增岗位信息
     *
     * @param jobInformation 岗位信息
     * @return 结果
     */
    @Override
    public int insertJobInformation(JobInformation jobInformation)
    {
        return jobInformationMapper.insertJobInformation(jobInformation);
    }

    /**
     * 修改岗位信息
     *
     * @param jobInformation 岗位信息
     * @return 结果
     */
    @Override
    public int updateJobInformation(JobInformation jobInformation)
    {
        return jobInformationMapper.updateJobInformation(jobInformation);
    }

    /**
     * 批量删除岗位信息
     *
     * @param jobIds 需要删除的岗位信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteJobInformationByJobIds(Integer[] jobIds) {
        for (Integer jobId : jobIds) {
            deleteJobExperiment(jobId);
        }
        return jobInformationMapper.deleteJobInformationByJobIds(jobIds);
    }

    private int deleteJobExperiment(Integer jobId){
        return jobInformationMapper.deleteCanDoById(jobId);
    }

    /**
     * 删除岗位信息信息
     *
     * @param jobId 岗位信息主键
     * @return 结果
     */
    @Override
    public int deleteJobInformationByJobId(Integer jobId)
    {
        deleteJobExperiment(jobId);
        return jobInformationMapper.deleteJobInformationByJobId(jobId);
    }

    @Override
    public List<CanDo> selectCanDo(CanDo canDo) {
        return jobInformationMapper.selectCanDo(canDo);
    }

    @Override
    public Integer insertCanDo(CanDo canDo) {
        return jobInformationMapper.insertCanDo(canDo);
    }

    @Override
    public Integer updateCanDo(CanDo canDo) {
        return jobInformationMapper.updateCanDo(canDo);
    }

    @Override
    public Integer deleteCanDo(Long id) {
        return jobInformationMapper.deleteCanDo(id);
    }

    @Override
    public List<BriefPost> getList() {
        return jobInformationMapper.selectList();
    }

    @Override
    public List<EJob> getExperimentTypes(Integer id) {
        return jobInformationMapper.selectExperimentTypes(id);
    }

    @Override
    public List<PExperiment> getExperiments(Experiment experiment) {
        return jobInformationMapper.selectExperiments(experiment);
    }

    @Override
    @Transactional
    public Boolean finishExperiment(FinishInfo finishInfo) {
        boolean f=false;
        List<String> unTranExperiment=new ArrayList<String>();
        List<String> unTranJob=new ArrayList<String>();
        Long sid = null;
        Map<Long,Integer> orders=new HashMap<>();
        for (Long id : finishInfo.getIds()) {
            Experiment experiment = experimentMapper.selectExperimentalManagementByExperimentId(id);
            orders.put(id,experiment.getOrderNum());
            sid = experiment.getSampleId();
            int status = experiment.getExpermentType();
            if (status < 100) {
                unTranJob.add(experiment.getSampleName());
                f=true;
            }
            if (status % 100 < 30){
                unTranExperiment.add(experiment.getSampleName());
                f=true;
            }
        }
        Long pid=null;
        if (!unTranExperiment.isEmpty() || !unTranJob.isEmpty()) {
             pid= experimentMapper.selectProjectId(sid);
        }
        if (!unTranExperiment.isEmpty()){
            Circulation circulation = new Circulation();
            circulation.setCirculationType(9);
            circulation.setProjectId(pid);
            circulation.setUserName(SecurityUtils.getUsername());
            circulation.setCirculationSuggestion("请求流转样品："+unTranExperiment.toString()+" 下的实验至项目负责人");
            experimentMapper.insertFlume(circulation);
        }
        if (!unTranJob.isEmpty()){
            Circulation circulation = new Circulation();
            circulation.setCirculationType(7);
            circulation.setProjectId(pid);
            circulation.setUserName(SecurityUtils.getUsername());
            circulation.setCirculationSuggestion("请求流转样品："+unTranJob.toString()+" 下的实验岗位至项目负责人");
            experimentMapper.insertFlume(circulation);
        }
        if (f) {
            return false;
        }
        for (Long id : finishInfo.getIds()) {
            Experiment experiment = new Experiment();
            experiment.setExperimentId(id);
            experiment.setExperimentPerson(finishInfo.getExperimentPerson());
            experiment.setExperimentEndDate(finishInfo.getExperimentEndDate());
            experiment.setOrderNum(orders.get(id)==0?0:2);
            jobInformationMapper.updateExperiment(experiment);
            jobInformationMapper.updateSampleConfirmStatusByExpId(1<<5,id);
        }
        return true;
    }

    @Override
    public void top(Tops tops) {
        for (Long id : tops.getIds()) {
            Experiment experiment = new Experiment();
            experiment.setExperimentId(id);
            experiment.setOrderNum(0);
            experiment.setTopReason(tops.getReason());
            jobInformationMapper.updateExperiment(experiment);
        }
    }
    @Override
    public void cancelTop(List<Long> ids) {
        for (Long id : ids) {
            Experiment experiment = new Experiment();
            experiment.setExperimentId(id);
            Experiment e = experimentMapper.selectExperimentalManagementByExperimentId(id);
            experiment.setOrderNum(e.getExperimentEndDate()==null?1:2);
            jobInformationMapper.updateExperiment(experiment);
        }
    }

    @Override
    public List<PExperiment> selectManagerExperiment() {
        return jobInformationMapper.selectManagerExperiments();
    }

    @Override
    public List<BriefPost> selectPostByType(Integer type) {
        return jobInformationMapper.selectPostByType(type);
    }

    @Override
    public void setJob(Long experimentId, Integer jobId) {
        jobInformationMapper.setJob(experimentId, jobId);
    }
}

